<template>
  <el-container class="layout-container">
    <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '64px' : '200px'" class="aside-container">
      <div class="logo-container">
        <img src="../assets/logo.png" alt="Logo" class="logo" />
        <span v-if="!isCollapse" class="logo-text">赏金猎人</span>
      </div>
      <el-menu
        :default-active="activeMenu"
        class="el-menu-vertical"
        :collapse="isCollapse"
        @select="handleSelect"
        background-color="#001529"
        text-color="#fff"
        active-text-color="#ffd04b"
      >
        <el-menu-item index="bounty">
          <el-icon><Coin /></el-icon>
          <template #title>悬赏</template>
        </el-menu-item>
        <el-menu-item index="message">
          <el-icon><ChatDotRound /></el-icon>
          <template #title>消息</template>
        </el-menu-item>
        <el-menu-item index="add-bounty">
          <el-icon><Plus /></el-icon>
          <template #title>发布悬赏</template>
        </el-menu-item>
        <el-menu-item index="cart">
          <el-icon><ShoppingCart /></el-icon>
          <template #title>购物车</template>
        </el-menu-item>
      </el-menu>
    </el-aside>
    
    <el-container>
      <!-- 头部 -->
      <el-header class="header-container">
        <div class="header-left">
          <el-button type="text" @click="toggleCollapse">
            <el-icon><Expand /></el-icon>
          </el-button>
        </div>
        <div class="header-right">
          <el-dropdown trigger="click" @command="handleCommand">
            <div class="user-info">
              <el-avatar :size="32" :src="userAvatar" />
              <span class="username">{{ username }}</span>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">查看用户详细信息</el-dropdown-item>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <el-main class="main-container">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import {
  ChatDotRound, 
  Plus, 
  ShoppingCart, 
  Expand,
  Coin
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const route = useRoute()
const isCollapse = ref(false)
const username = ref('用户名')
const userAvatar = ref('')

// 计算当前激活的菜单项
const activeMenu = computed(() => {
  const path = route.path
  const pathArray = path.split('/')
  return pathArray[pathArray.length - 1]
})

// 切换侧边栏折叠状态
const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

// 处理菜单选择
const handleSelect = (key: string) => {
  router.push(`/home/${key}`)
}

// 处理下拉菜单命令
const handleCommand = (command: string) => {
  if (command === 'profile') {
    router.push('/user-profile')
  } else if (command === 'logout') {
    localStorage.removeItem('token')
    ElMessage.success('退出登录成功')
    router.push('/')
  }
}

// 获取用户信息
const getUserInfo = async () => {
  try {
    const token = localStorage.getItem('token')
    if (token) {
      // 这里应该调用获取用户信息的API
      // 模拟数据
      username.value = '张三'
      userAvatar.value = 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'
    }
  } catch (error) {
    console.error('获取用户信息失败', error)
  }
}

onMounted(() => {
  getUserInfo()
})
</script>

<style scoped lang="scss">
.layout-container {
  height: 100vh;
  
  .aside-container {
    background-color: #001529;
    transition: width 0.3s;
    overflow: hidden;
    
    .logo-container {
      height: 60px;
      display: flex;
      align-items: center;
      padding: 0 10px;
      color: #fff;
      
      .logo {
        width: 32px;
        height: 32px;
        margin: 0 10px 0 10px;
        border-radius: 100%;
      }
      
      .logo-text {
        font-size: 18px;
        font-weight: bold;
        white-space: nowrap;
      }
    }
    
    .el-menu-vertical {
      border-right: none;
      margin-left: 6%;
      &:not(.el-menu--collapse) {
        width: 200px;
      }
    }
  }
  
  .header-container {
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
    
    .header-left {
      display: flex;
      align-items: center;

    }
    
    .header-right {
      .user-info {
        display: flex;
        align-items: center;
        cursor: pointer;
        
        .username {
          margin-left: 8px;
          font-size: 14px;
        }
      }
    }
  }
  
  .main-container {
    background-color: #f0f2f5;
    padding: 20px;
  }
}
</style>